<template>
  <div class="list" ref='wrapper'>
      <div class="content">
          <div class="area" ref='origin'>
              <div class="title border-topbottom">您的位置</div>
              <div class="button-list">
                  <div class="button-wrapper">
                      <div class="button">{{city}}</div>
                  </div>
              </div>
          </div>
          <div class="area">
              <div class="title border-topbottom">热门城市</div>
              <div class="button-list">
                  <div class="button-wrapper" v-for="item of hotCities" :key='item.id' @click='change(item.name)'>
                      <div class="button">{{item.name}}</div>
                  </div>
              </div>
          </div>
          <div class="area" v-for="item of letters" :key='item' :ref='item'>
              <div class="title border-topbottom">{{item}}</div>
              <ul class='item-list'>
                <li class='item border-bottom' v-for="innerItem of cities[item]" :key='innerItem.id' @click='change(innerItem.name)'>
                    {{innerItem.name}}
                </li>
              </ul>
          </div>
      </div>
  </div>
</template>

<script>
import {mapState, mapMutations} from 'vuex'
import Bscroll from 'better-scroll'
export default {
  name: 'CityList',
  props: {
    cities: Object,
    hotCities: Array,
    letters: Array,
    letter: String
  },
  data () {
    return {
      scroll: null
    }
  },
  computed: {
    ...mapState(['city'])
  },
  methods: {
    change (value) {
      this.changeCity(value)
      this.$router.push('/')
      this.scroll.scrollToElement(this.$refs.origin)
    },
    ...mapMutations(['changeCity'])
  },
  watch: {
    letter (newVal) {
      const element = this.$refs[newVal][0]
      this.scroll.scrollToElement(element)
    }
  },
  activated () {
    const wrapper = this.$refs.wrapper
    this.scroll = new Bscroll(wrapper, {
      click: true
    })
  }
}
</script>

<style lang="stylus" scoped>
@import '~@/assets/styles/varibles.styl';
.border-topbottom
  &:before
    border-color #ccc
  &:after
    border-color #ccc
.border-bottom
  &:before
    border-color #ccc
.list
  position absolute
  top 1.58rem
  left 0
  right 0
  bottom 0
  overflow hidden
  font-size .26rem
  .title
    line-height .54rem
    background #eee
    color #666
    padding-left .2rem
  .button-list
    overflow hidden
    padding .1rem .6rem .1rem .2rem
    .button-wrapper
      width 33.33%
      float left
      .button
        margin .1rem
        text-align center
        padding .1rem
        border .02rem solid #ccc
        border-radius .06rem
  .item-list
    .item
      line-height .76rem
      padding-left .2rem
</style>>
